
package com.o2o.config.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurationSelector;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import javax.xml.crypto.Data;


/**
 * package_name:com.o2o.config.service
 * Author:徐亚远
 * Date:2020/1/5 20:02
 * 项目名:com.o2o
 * Desription:
 **/
@Configuration
//首先使用注解@EnableTransactionManagment 开启事务支持
//在service方法上添加注解@Transcational便可
@EnableTransactionManagement
public class TransactionManagmentConfiguration implements TransactionManagementConfigurer {
    @Autowired
    //注入DataSourceConfiguration里边的dataSource,通过createDataSource()获取
    private DataSource dataSource;

    /*
     * 关于事务管理,需要返回PlatformTransactionManager的实现
     * */
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }
}

